#include <fstream>
#include "vol.h"

using namespace vol;

ifstream file;

double get_num(){
	char ch = '\0';
	double tmp_num = 0;
	bool tmp_neg = false;
	bool decimal = false;
	int dec_len = 0;
	for (; (!file.eof()) && (!isdigit(ch)) && (ch != '-'); file.get(ch));
	while (!file.eof()){
		if (isdigit(ch)){
			if (decimal) dec_len++;
			tmp_num = tmp_num * 10 + ch - 48;
		}else if (ch == '-') tmp_neg = true;
		else if (ch == '.')	decimal = true;
		else{
			if (tmp_neg) tmp_num = -tmp_num;
			if (dec_len > 0) tmp_num /= pow((double)10, dec_len);
			return tmp_num;
		}
		file.get(ch);
	}
	return 0;
}

int main(int argc, char *argv[]){

	if (argc == 1){
		cout << "No input file." << endl;
		cout << "USAGE: " << argv[0] << " <input-file>" << endl;
		return 1;
	}

	file.open(argv[1]);

	if (!file.is_open()){ cout << "Cant open file." << endl; return 1; }

	int rows = (int)get_num();
	int cols = (int)get_num();

	polytope p(rows, cols);

	cout << "Hyperplanes: " << rows << endl << "Dimensions:" << cols << endl;

	for (int i = 0; i < rows; i++){
		p.vecb(get_num(), i);
		for (int j = 0; j < cols; j++) p.matA(get_num(), i, j);
	}

	p.msg_off = true;
	p.check_planes_off = true;
	//cout << endl << "======= Preprocessing =======" << endl;
	p.Preprocess();
	//cout << endl << "========= Sampling ==========" << endl;
	//cout << endl << "Volume of polytope (estimate): " << p.EstimateVol() << endl;
	//ofstream outfile("CPVE.result", ios::app);
	//outfile << p.Volume() << endl;
	//outfile.close();


	ofstream outfile("out141.dat");
	for (int i = 0; i < 5000; i++){
		if (i % 20 == 0) cout << i << endl;
		p.EstimateVol();
		outfile << p.Volume() << endl;
	}
	outfile.close();

	//print matrix A and vector b which applied affine transformation
/*
	for (int i = 0; i < rows; i++){
		cout << p.vecb(i) << ' ';
		for (int j = 0; j < cols; j++) cout << p.matA(i, j) << ' ';
		cout << endl;
	}
*/

	return 0;
}
